package com.cmpanyandi.elearning.wordsdriller.activity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.cmpanyandi.elearning.utils.db.DbAdapter;
import com.cmpanyandi.elearning.wordsdriller.model.Configuration;
import com.cmpanyandi.elearning.wordsdriller.model.Dictionary;
import com.cmpanyandi.elearning.wordsdriller.model.DictionaryItem;

/**
 * Main activity class.
 * 
 * @author wojtek
 * 
 */
public class DictionaryList extends DictionaryActivityList {

    private static final int ADD_DICTIONARY = Menu.FIRST;
    private static final int DELETE_DICTIONARY = Menu.FIRST + 1;
    private ImageView addDictionary;
   
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
	setContentView(R.layout.dictionary_list);
	super.onCreate(savedInstanceState);
	/** initilise database connection */
	initDatabase();

	fillData();
	
	addDictionary = (ImageView) findViewById(R.id.mainPlus);
	addDictionary.setOnClickListener(new View.OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		createDictionary();
		
	    }
	});
    }
    
    
    private void initDatabase() {
	DbAdapter.init(this, Configuration.DEFAULT_DICTIONARY_DB);
    }

    private void fillData() {
	Cursor cursor = DbAdapter.getInstance().fetchBeans(new Dictionary());
	startManagingCursor(cursor);

	String[] from = new String[] { Dictionary.NAME, Dictionary.DESCRIPTION };

	int[] to = new int[] { R.id.toptext, R.id.bottomtext };

	SimpleCursorAdapter dictionaries = new SimpleCursorAdapter(this, R.layout.dictionary_row, cursor, from, to);
	setListAdapter(dictionaries);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	super.onCreateOptionsMenu(menu);
	menu.add(0, ADD_DICTIONARY, 0, R.string.menu_add_dictionary);
	return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
	switch (item.getItemId()) {
	case ADD_DICTIONARY:
	    createDictionary();
	    return true;
	}

	return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	super.onCreateContextMenu(menu, v, menuInfo);
	menu.add(0, DELETE_DICTIONARY, 0, R.string.menu_delete_dictionary);

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	switch (item.getItemId()) {
	case DELETE_DICTIONARY:

	    DbAdapter.getInstance().delete(new Dictionary(info.id));
	    fillData();
	    return true;

	}
	return super.onContextItemSelected(item);
    }

    private void createDictionary() {
	Intent i = new Intent(this, DictionaryEdit.class);
	startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
	super.onListItemClick(l, v, position, id);
	Intent intent = new Intent(this, DictionaryItemSearch.class);
	intent.putExtra(DictionaryItem.DICTIONARY, id);
	startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
	super.onActivityResult(requestCode, resultCode, intent);
	fillData();
    }

}